#!/usr/bin/perl

#====  PROJECT =================================================================
#         NAME:  Lattice3d
#      PURPOSE:  Test Case for a simple 3 dimensional lattice network.
#      RETURNS:  True
#===============================================================================


# Initialisation: Simple 3d Lattice Network.

use insj_m_network;

# Create the network.
	$network = insj_m_network->new(name => "Lattice3d Network.");

	$x_axis = 32;
	$y_axis = 32;
	$z_axis = 4;

	# Initialize Nodes.
	for($x = 1; $x <= $x_axis; $x++){
		for($y = 1; $y <= $y_axis; $y++){
			for($z = 1; $z <= $z_axis; $z++){
				$node = $network->create_node( name=> $x.";".$y.";".$z );
				$node -> set_state("VirusA", 0, 0);
				}
			}		
		}

	# Connect Nodes.
	for($x = 1; $x <= $x_axis; $x++){
		for($y =1; $y <= $y_axis; $y++){
			for($z =1; $z <= $z_axis; $z++){
		
				$node = $network->get_node( $x.";".$y.";".$z );

				# X-Axis
				if    ($x == 1) {
					$node -> add_node($network -> get_node( ($x+1).";".$y.";".$z ) );
					}
				elsif ($x == $x_axis) {
					$node -> add_node($network -> get_node( ($x-1).";".$y.";".$z ) );				
					}
				else {
					$node -> add_node($network -> get_node( ($x+1).";".$y.";".$z ) );
					$node -> add_node($network -> get_node( ($x-1).";".$y.";".$z ) );
					}

				# Y-Axis
				if    ($y == 1) {
					$node -> add_node($network -> get_node( $x.";".($y+1).";".$z ) );
					}
				elsif ($y == $y_axis) {
					$node -> add_node($network -> get_node( $x.";".($y-1).";".$z ) );
					}
				else {
					$node -> add_node($network -> get_node( $x.";".($y+1).";".$z ) );
					$node -> add_node($network -> get_node( $x.";".($y-1).";".$z ) );
					}

				# Z-Axis
				if    ($z == 1) {
					$node -> add_node($network -> get_node( $x.";".$y.";".($z+1) ) );
					}
				elsif ($z == $z_axis) {
					$node -> add_node($network -> get_node( $x.";".$y.";".($z-1) ) );
					}
				else {
					$node -> add_node($network -> get_node( $x.";".$y.";".($z+1) ) );
					$node -> add_node($network -> get_node( $x.";".$y.";".($z-1) ) );
					}

				}
			}		
		}

# Infect a single node.
	$node = $network->get_node( "4;4;4" );
	$node -> set_state("VirusA", 1,1); $node -> set_state("VirusA_t", 1,1);

# Evolve network, transmission probability 10%.
	for($generation = 2; $generation <=250; $generation++){
		$network->rewind;
		
		while ( $node = $network -> get_next ){
			
			$node->rewind;

			while ( $edge = $node -> get_next ){
				#The Ruleset.
				if ( $edge -> get_state("VirusA") == 1 ) {
					if ( rand(100) <= 50 and $node->get_state("VirusA") == 0) { $node->set_state("VirusA_t", 1, $generation); 
						}
					}
				}
			}
		# Temporay states to actual states!
		$network->rewind;
		
		while ( $node = $network -> get_next ){
			if ( $node->get_state("VirusA") != $node->get_state("VirusA_t") ) { 
				$node->set_state("VirusA", $node->get_state("VirusA_t"), $generation);  
				}			
			}

		
	}
$network->print;

$network->export("test_lattice3d.gexf", "gephi");

1;

